﻿package cn.antscript.airCMS.display 
{
	import cn.antscript.airCMS.display.component.WaitBar;
	import flash.display.NativeWindow;
	import flash.display.NativeWindowInitOptions;
	import flash.display.NativeWindowSystemChrome;
	import flash.display.NativeWindowType;
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.NetStatusEvent;
	import flash.events.SecurityErrorEvent;
	import flash.events.TimerEvent;
	import flash.media.Video;
	import flash.net.NetConnection;
	import flash.net.NetStream;
	import flash.utils.Timer;
	
	/**
	 * ...
	 * @author antscript
	 */
	public class VideoViewer extends NativeWindow
	{
		
		private var _container:Sprite;
		private var _video:Video;
		
		private static var _instance:VideoViewer;
		
		private var _connection:NetConnection;
		private var _netStream:NetStream;
		
		private var _client:MyClient;
		
		
		public function VideoViewer(privateClass:MyClient) 
		{
			_client = privateClass;
			
			var opt:NativeWindowInitOptions = new NativeWindowInitOptions();
			opt.resizable = false;
			opt.type = NativeWindowType.UTILITY;
			opt.systemChrome = NativeWindowSystemChrome.ALTERNATE;
			super(opt);
			this.alwaysInFront = true;
			
			
			_video = new Video();
			this.stage.addChild(_video);
			this.stage.align = StageAlign.TOP_LEFT;
			this.stage.scaleMode = StageScaleMode.NO_SCALE;
			this.stage.stageWidth = 500;
			this.stage.stageHeight = 380;
			
			_connection = new NetConnection();	
			_connection.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
            _connection.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
            _connection.connect(null);		
			
			
            addEventListener(Event.CLOSING, onClose);
        }

        private function netStatusHandler(event:NetStatusEvent):void {
            switch (event.info.code) {
                case "NetConnection.Connect.Success":
                    connectStream();
                    break;
                case "NetStream.Play.StreamNotFound":
                    trace("Stream not found: ");
					visible = false;
                    break;
				case "NetStream.Play.Start":				
					WaitBar.getInstance().hide();
				break;
				case "NetStream.Play.Stop":
					visible = false;
				break;
				case "NetStream.Buffer.Empty":
				WaitBar.getInstance().show(this.stage);
				break;
				case "NetStream.Buffer.Full":				
				var time:Timer = new Timer(100, 1);
				time.addEventListener(TimerEvent.TIMER_COMPLETE, onCom);
				time.start();
				break;


            }
        }
		
		private function onCom(e:TimerEvent):void 
		{
			stage.stageWidth = _client.width;
			stage.stageHeight = _client.height;
			_video.width = stage.stageWidth ;
			_video.height = stage.stageHeight;
			WaitBar.getInstance().hide();
			
		}

        private function securityErrorHandler(event:SecurityErrorEvent):void {
            trace("securityErrorHandler: " + event);
        }

        private function connectStream():void {
			_netStream = new NetStream(_connection);
			_netStream.client = _client;
			_netStream.bufferTime = 5;
			_netStream.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);		
			_video.attachNetStream(_netStream);
        }

		
		
		
		private function onClose(e:Event):void 
		{
			e.preventDefault();
			_netStream.close();
			visible = false;
		}
		
		public static function getInstance():VideoViewer {
			if (_instance == null) {
				_instance = new VideoViewer(new MyClient());
			}
			return _instance;
		}
		
		public function play(url:String):void {
			
			WaitBar.getInstance().show(this.stage);
			
			_netStream.play(url);			
			activate();
			
			
		}
		
		
		
		
		public function closeWin():void {			
			close();
		}
		
	}

}

class MyClient {
	
	internal var width:int;
	internal var height:int;
	
    public function onMetaData(info:Object):void {
		width = info.width;
		height = info.height;
		//trace("metadata: duration=" + info.duration + " width=" + info.width + " height=" + info.height + " framerate=" + info.framerate);
    }
    public function onCuePoint(info:Object):void {
       // trace("cuepoint: time=" + info.time + " name=" + info.name + " type=" + info.type);
    }
}

